<template>
    <div class="register">
        <div class="container">
            <h2 class="title">注册</h2>
            <div class="form">
                <div class="group">
                    <input v-model="form.username" type="text" placeholder="用户名" class="field">
                </div>
                <div class="group">
                    <input v-model="form.password" type="password" placeholder="密码" class="field">
                </div>
                <div class="group">
                    <input v-model="form.email" type="email" placeholder="邮箱" class="field">
                </div>
                <button class="btn" @click="handleRegister">注册</button>
                <div class="link">
                    已有账号？<span @click="goToLogin">去登录</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const form = ref({
    username: '',
    password: '',
    email: ''
})

// 生成简单token
const generateToken = () => {
    return 'token-' + Math.random().toString(36) + Date.now().toString(36)
}

const handleRegister = () => {
    if (!form.value.username || !form.value.password) {
        alert('用户名和密码不能为空')
        return
    }

    // 获取现有用户数据或初始化
    const users = JSON.parse(localStorage.getItem('users') || '[]')

    // 检查用户名是否已存在
    if (users.some(user => user.username === form.value.username)) {
        alert('用户名已存在')
        return
    }

    // 添加新用户
    const newUser = {
        ...form.value,
        token: generateToken(),
        createdAt: new Date().toISOString()
    }
    users.push(newUser)

    // 保存到localStorage
    localStorage.setItem('users', JSON.stringify(users))
    alert('注册成功！')
    router.push('/main')
}

const goToLogin = () => {
    router.push('/login')
}
</script>

<style lang="css" scoped>
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

.register {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-image: url('../assets/img/photo.png');
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
    padding: 20px;
}

.container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    max-width: 420px;
    background: rgba(255, 246, 246, 0.85);
    backdrop-filter: blur(8px);
    border-radius: 24px;
    font-family: 'Noto Sans JP', sans-serif;
    color: #791919;
    box-shadow:
        0 0 15px 5px rgba(255, 246, 246, 0.6),
        0 10px 25px rgba(121, 25, 25, 0.1);
    padding: 30px;
    animation: fadeIn 1s ease-out;
}

.title {
    font-size: 32px;
    font-weight: bold;
    margin-bottom: 30px;
    text-shadow: 2px 2px 4px rgba(107, 33, 97, 0.3);
}

.form {
    width: 100%;
}

.group {
    margin-bottom: 20px;
}

.field {
    width: 100%;
    padding: 15px 20px;
    border: 2px solid rgba(121, 25, 25, 0.3);
    border-radius: 50px;
    font-size: 16px;
    font-family: 'Noto Sans JP', sans-serif;
    background: rgba(255, 255, 255, 0.8);
    color: #333;
    transition: all 0.3s ease;
}

.field:focus {
    outline: none;
    border-color: #791919;
    box-shadow: 0 0 0 3px rgba(121, 25, 25, 0.2);
}

.btn {
    width: 100%;
    padding: 15px;
    margin-top: 10px;
    background: linear-gradient(135deg, #791919, #a52a2a);
    color: white;
    border: none;
    border-radius: 50px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(121, 25, 25, 0.2);
}

.link {
    text-align: center;
    margin-top: 20px;
    font-size: 14px;
}

.link span {
    color: #a52a2a;
    cursor: pointer;
    text-decoration: underline;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@media (max-width: 480px) {
    .container {
        padding: 20px;
        max-width: 360px;
    }

    .title {
        font-size: 28px;
    }
}
</style>